import logging
import sys

class SimpleColoredFormatter(logging.Formatter):
    """简单的彩色日志格式器"""

    # 颜色映射
    LEVEL_COLORS = {
        'DEBUG': '\033[36m',  # 青色
        'INFO': '\033[32m',  # 绿色
        'WARNING': '\033[33m',  # 黄色
        'ERROR': '\033[31m',  # 红色
        'CRITICAL': '\033[35m',  # 紫色
    }
    RESET = '\033[0m'

    def format(self, record):
        # 获取原始格式化结果
        formatted = super().format(record)

        # 添加颜色
        color = self.LEVEL_COLORS.get(record.levelname, '')
        if color:
            # 只给日志级别添加颜色
            formatted = formatted.replace(
                record.levelname,
                f"{color}{record.levelname}{self.RESET}"
            )

        return formatted

# 替换你的配置
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# 创建处理器
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(SimpleColoredFormatter(
    '%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s'
))

logger.addHandler(handler)
